home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2079 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: nntphub.cb.att.com!news
  2. From: arvind@cbgbcs.att.com (Arvind Sharma)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is there subroutine in C?
  5. Date: Thu, 18 Jan 1996 14:24:59 GMT
  6. Organization: AT&T Bell Laboratories, Columbus, Ohio
  7. Message-ID: <4dmkrd$l35@nntpa.cb.att.com>
  8. References: <4an13a$d94@news.ust.hk>
  9. NNTP-Posting-Host: arvind586.cb.att.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. hclau@uxmail.ust.hk (S. Vegata) wrote:
  13.  
  14.  
  15. >Hello All,
  16.  
  17. >I 'd like to ask if there is subroutine in C and if subroutine
  18. >can change the passed arguments.   For example:
  19.  
  20. >main()
  21. >{
  22. >int x,y;
  23. >x = 5; y = 10;
  24. >add_one(x,y);
  25.  
  26. Use this instead
  27.  
  28. add_one(&x, &y);  /* Basically pass the address of the variables where the
  29. "value" is stored */
  30.  
  31. >printf("x: %d y: %d\n",x,y);
  32. >}
  33.  
  34. >add_one(x,y) /* subroutine ?? */
  35. Declare this way
  36. int *x, *y;  /* integer pointers */
  37. >int x,y;
  38. >{
  39. >x = x + 1; y = y + 1;
  40. Change to
  41. *x = *x +1;
  42. *y = *y +1;
  43. >}
  44.  
  45. >After running this program, x is still 5 and y is still 10.
  46. >I am not familiar with C and the "passing by value" in C
  47. >really troubles me.
  48.  
  49. >Could anyoone kindly tell me how I should write the correct
  50. >subroutine and argument passing such that x and y will become
  51. >6 and 11 respectively after the subrountine add_one(x,y)?
  52.  
  53. >Thank you very much
  54.  
  55. >best regards,
  56.  
  57. >Eric Lau.
  58.  
  59. Arvind Sharma
  60. arvind@cbgbcs.att.com
  61. Columbus, OH, USA
  62.  
  63.